<?php

namespace App\Http\Controllers\Home;

use App\Alipay\Alipay;
use App\Helpers\VideoParser;
use App\Http\Controllers\Controller;
use App\Library\Messager;
use App\Models\BlockItem;
use App\Models\Item;
use App\Models\ItemCatlog;
use App\Models\ItemPush;
use App\Models\PostItem;
use App\Models\Trade;
use App\Models\Wallet;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class IndexController extends Controller
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        return redirect('h5');
        $this->assign([
            'newsList'=>PostItem::available()->limit(3)->orderByDesc('aid')->get(),
            //幻灯
            'slideList'=>BlockItem::whereBlockId(1)->orderBy('displayorder')->limit(5)->get(),
            'catlogList'=>ItemCatlog::fetchWithCache()
        ]);

        $qiangGou = Cache::remember('QiangGou', 5, function (){
            $items = ItemPush::with('item')->whereHas('item', function ($query){
                return $query->onSale();
            })->where('groupid', 4)->limit(5)->get()->map(function ($item){
                return $item->item;
            });
            return $items;
        });

        //优选
        $youxuanList = ItemPush::with('item')->whereHas('item', function ($query){
            return $query->onSale();
        })->where('groupid', 3)->limit(10)->orderByDesc('id')
            ->get()->map(function ($push){
                return $push->item;
        });

        //水果
        $fruitsIds = ItemCatlog::fetchAllChildIds(131);
        $fruits = Item::whereIn('catid', $fruitsIds)->onSale()->limit(6)->orderByDesc('sold')->get();
        //蔬菜
        $vegetablesIds = ItemCatlog::fetchAllChildIds(47);
        $vegetables = Item::whereIn('catid', $vegetablesIds)->onSale()->limit(6)->orderByDesc('sold')->get();
        //副食
        $fushiIds = ItemCatlog::fetchAllChildIds(50);
        $fushi = Item::whereIn('catid', $fushiIds)->onSale()->limit(6)->orderByDesc('sold')->get();

        $this->assign(compact('youxuanList','fruits', 'vegetables', 'fushi', 'qiangGou'));

        return $this->view('home.index');
    }

    public function upgrade(){

    }

    public function test()
    {
        //$json = '{"appid":"wx68fe44528075f3cb","bank_type":"CFT","cash_fee":"1","fee_type":"CNY","is_subscribe":"Y","mch_id":"1386271002","nonce_str":"154db8abcb829670a1038e81c458b93d","openid":"osROLv0hZevV2S0PutGj6jUUgl4k","out_trade_no":"615355042183358383","result_code":"SUCCESS","return_code":"SUCCESS","sign":"DD872BECA83096802669175DE1056554","time_end":"20180829090143","total_fee":"1","trade_type":"JSAPI","transaction_id":"4200000194201808292023808725"}';
        //print_array(json_decode($json, true));

        //dd($this->request->user());


        $str = encrypt('1000000');
        echo '<p>'.$str.'</p>';
        echo '<p>'.decrypt($str).'</p>';

    }

    public function app(){

        $platform = '';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
             $platform = 'ios';
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
            if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')){
                $platform = 'weixin';
            }else {
                return redirect()->to('https://cugeng.cn/apk/lastest.apk');
            }
        }else{
            return '<h3 style="text-align: center;">请用手机扫码打开此链接</h3>';
        }

        $this->assign(['platform'=>$platform]);
        return $this->view('home.app');
    }
}
